package com.forum.service.other;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.vo.ConfessionCommentInfoVo;
import com.common.vo.ConfessionWallCommentReplyVo;
import com.forum.mapper.confession.ConfessionWallCommentMapper;
import com.forum.mapper.confession.ConfessionWallCommentReplyMapper;
import com.forum.mapper.confession.ConfessionWallMapper;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.mapper.talk.TalkPostsCommentMapper;
import com.forum.mapper.talk.TalkPostsCommentReplyMapper;
import com.forum.pojo.confession.ConfessionWall;
import com.forum.pojo.confession.ConfessionWallComment;
import com.forum.pojo.confession.ConfessionWallCommentReply;
import com.forum.pojo.forum.ForumUser;
import com.forum.pojo.talk.TalkPostsComment;
import com.forum.pojo.talk.TalkPostsCommentReply;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tsing
 * 线程服务
 */
@Service

public class ThreadService {

    /**
     * 更改表白墙浏览量
     * **/
    @Async("taskExecutor")
    public void updateConfessionBrowse(ConfessionWallMapper confessionWallMapper, ConfessionWall confessionWall){
        LambdaQueryWrapper<ConfessionWall> queryWrapper = new LambdaQueryWrapper<>();
        Integer browse = confessionWall.getBrowse();
        queryWrapper.eq(ConfessionWall::getBrowse,browse);
        confessionWall.setBrowse(browse+1);
        confessionWallMapper.update(confessionWall, queryWrapper);
    }

    /**
     * 获取指定表白墙下的评论和评论的回复
     * **/
    @Async("taskExecutor")
    public List<ConfessionCommentInfoVo> getCommentByConfession(ConfessionWallCommentMapper confessionCommentMapper, ForumUserMapper forumUserMapper, ConfessionWallCommentReplyMapper replyMapper,Integer confessionId){
        LambdaQueryWrapper<ConfessionWallComment> commentQueryWrapper = new LambdaQueryWrapper<>();
        commentQueryWrapper.eq(ConfessionWallComment::getConfessionId,confessionId);

        List<ConfessionWallComment> confessionWallComments = confessionCommentMapper.selectList(commentQueryWrapper);
        List<ConfessionCommentInfoVo> commentInfoVos = confessionWallComments.stream().map(comment -> {

            ForumUser user = forumUserMapper.selectById(comment.getPublisherId());
            ConfessionCommentInfoVo commentInfoVo = new ConfessionCommentInfoVo();
            LambdaQueryWrapper<ConfessionWallCommentReply> queryWrapper = new LambdaQueryWrapper<>();
            BeanUtils.copyProperties(comment, commentInfoVo);
            commentInfoVo.setPublisherAvatar(user.getAvatar());

            //获取评论下的所有回复 默认获取十条
            queryWrapper.eq(ConfessionWallCommentReply::getCommentId,comment.getId()).last("limit 10");
            List<ConfessionWallCommentReply> confessionWallCommentReplies = replyMapper.selectList(queryWrapper);

            List<ConfessionWallCommentReplyVo> replyVos = confessionWallCommentReplies.stream().map(reply -> {
                ConfessionWallCommentReplyVo replyVo = new ConfessionWallCommentReplyVo();
                BeanUtils.copyProperties(reply,replyVo);
                //获取发布者昵称和头像
                ForumUser publisher = forumUserMapper.selectById(reply.getPublisherId());
                replyVo.setPublisherName(publisher.getNickname());
                //获取被回复者昵称
                ForumUser parentUser = forumUserMapper.selectById(reply.getParentUserId());
                replyVo.setParentUserName(parentUser.getNickname());
                return replyVo;
            }).collect(Collectors.toList());


            ForumUser forumUser = forumUserMapper.selectById(comment.getPublisherId());
            commentInfoVo.setNickname(forumUser.getNickname());
            commentInfoVo.setReplyList(replyVos);

            return commentInfoVo;
        }).collect(Collectors.toList());
        return commentInfoVos;
    }

}
